﻿
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.Windows
Imports Autodesk.AutoCAD.Windows
Imports System.Windows.Forms
Imports System.Security.Permissions.SecurityPermission

Public Class AddBlock



    Private Sub AddBlock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        'Search for Blocks in the drawing
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
        Dim db As Database
        db = HostApplicationServices.WorkingDatabase
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(db)
        Dim Ids As ObjectId
        Ids = db.BlockTableId


        Using acTrans As Transaction = db.TransactionManager.StartTransaction()
            '' Open the Block table record for read
            Dim acBlkTbl As BlockTable
            acBlkTbl = acTrans.GetObject(db.BlockTableId, _
                                         OpenMode.ForRead)

            '' Open the Block table record Model space for read
            Dim acBlkTblRec, acBlkTblRec2 As BlockTableRecord
            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                            OpenMode.ForRead)
            acBlkTblRec2 = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), _
                                           OpenMode.ForRead)
            Dim countitem As String

            countitem = acBlkTblRec.Name

            Dim nCnt As Integer = 0

            '' Step through each object in Model space and
            '' display the type of object found
            Dim acref As BlockReference

            For Each acObjId As ObjectId In acBlkTblRec
                MsgBox(acObjId.ObjectClass().AppName)

                nCnt = nCnt + 1
            Next
            For Each acObjId As ObjectId In acBlkTblRec2
                MsgBox(acObjId.ObjectClass().DxfName)

                nCnt = nCnt + 1
            Next

            '' If no objects are found then display the following message
            If nCnt = 0 Then
                acDoc.Editor.WriteMessage(vbLf & "  No objects found")
            End If

            '' Dispose of the transaction
        End Using


    End Sub
End Class